<template>
    <div class="auth-dialog">
        <div class="welcome-text">
            <div>发布者登录</div>
            <div>享有更多曝光及资源</div>
        </div>

        <div class="input-box">
            <nut-form ref="formRef" :model-value="user" :rules="rules">
                <nut-form-item prop="email">
                    <nut-input v-model="user.email" placeholder="邮箱地址"></nut-input>
                </nut-form-item>

                <nut-form-item prop="password">
                    <nut-input v-model="user.password" type="password" placeholder="包含英文+数字8位以上密码"></nut-input>
                </nut-form-item>
            </nut-form>

            <div class="submit-btn">
                <nut-button block type="primary" @click="handleSubmit">
                    登录
                </nut-button>
            </div>

            <div class="or-divider">
                <nut-divider> 或者</nut-divider>
            </div>

            <div class="forgot-password">
                找回密码
            </div>

            <div class="footer-text">
                <p>本站接受RECAPTCHA以及GOOGLE</p>
                <p>
                    隐私声明和服务条款保护 <a href="">隐私权声明</a> 及
                    <a href="">服务条款</a>
                </p>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { axiosService } from '@/http/sever'
import { showNotify } from '@nutui/nutui'
const formRef = ref()

const user = ref({
    email: '',
    password: ''
})

const rules = ref({
    email: [
        { required: true, message: '请输入邮箱地址' },
        {
            validator: (val: string) =>
                /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(val),
            message: '请输入正确的邮箱格式'
        }
    ],
    password: [
        { required: true, message: '请输入密码' },
        {
            validator: (val: string) =>
                /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/.test(val),
            message: '密码格式不正确'
        }
    ]
})

// 表单提交处理
const handleSubmit = async () => {
    formRef.value
        ?.validate()
        .then(({ valid, errors }: { valid: boolean; errors: any }) => {
            if (valid) {
                axiosService.post('/api/publisher/Auth/login', user.value).then(res => {
                    showNotify.success('提交成功')
                    close()
                }).catch(err => {
                    showNotify.danger(err.msg || '登录失败')
                })
            } else {
                console.warn('error:', errors)
            }
        })
}
</script>

<style lang="scss" scoped>
.auth-dialog {
    width: 100%;
    height: 100%;
    background-color: #3f3f3f;
    padding-bottom: 30px;
    padding-top: 10px;
    position: relative;

    .tab {
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: 100%;
        height: 40px;

        &>div {
            flex: 1;
            background: #000000;
            height: 100%;
            line-height: 40px;
            text-align: center;
            color: #fff;
            font-weight: 600;
            font-size: 16px;
            position: relative;
            // transition: all 0.1s ease;

            &::after {
                content: "";
                display: block;
                width: 30px;
                height: 2px;
                background: #e53156;
                position: absolute;
                bottom: 2px;
                left: 50%;
                transform: translateX(-50%);
                opacity: 0;
            }
        }

        .active {
            background: transparent !important;

            &::after {
                opacity: 1;
            }
        }
    }

    .welcome-text {
        text-align: center;
        color: #fff;
        margin: 10px 0;

        &>div:first-child {
            font-size: 18px;
            font-weight: 600;
        }

        &>div:last-child {
            font-size: 10px;
        }
    }

    .input-box {
        padding: 0 13px;

        :deep(.nut-cell-group__wrap) {
            background: transparent;
            box-shadow: none;
            margin-bottom: 0;

            .nut-form-item {
                margin-bottom: 21px;
                background: #000;
                border-radius: 5px;
                padding-left: 12px;
                position: relative;

                &::after,
                &::before {
                    display: none;
                }

                input {
                    transform: translateY(1px);
                }

                .nut-form-item__body__tips {
                    position: absolute;
                    top: 100%;
                    left: 12px;
                    width: 100%;
                    transform: translateY(1px);
                }
            }
        }

        .submit-btn {
            margin-top: 5px;
            padding: 0 20px;

            .nut-button {
                font-weight: bold;
                background-image: linear-gradient(to right, #6dc0f9, #d308fe);
                letter-spacing: 1px;
            }
        }

        .or-divider {
            padding: 0 10px;
            text-align: center;

            .nut-divider {
                color: #777;
                margin: 7px 0;
            }
        }

        .forgot-password {
            text-align: center;
            color: #fff;
            font-size: 14px;
        }

        .footer-text {
            text-align: center;
            color: #d7d7d7;
            font-size: 10px;
            margin-top: 60px;
            line-height: 16px;

            a {
                color: #fff;
                font-weight: bold;
            }
        }
    }
}
</style>
